﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibBinary.FileData
{
    public class ArrayData : AbstractFileData
    {
        public AbstractFileData[] Data;

        public ArrayData(AbstractFileData parent)
            : base(parent)
        {
        }

        public override AbstractFileData GetChild(int index)
        {
            if (index < 0 || index >= Data.Length)
            {
                throw new ReadingFormatException("Wrong index in ArrayData.");
            }
            return Data[index];
        }

        public override string ToString()
        {
            if (Data.Length == 0)
            {
                return "[]";
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("[ ");
            foreach (var item in Data)
            {
                if (item == null)
                {
                    sb.Append("<empty>");
                }
                else
                {
                    sb.Append(item.ToString());
                }
                sb.Append(", ");
            }
            sb.Remove(sb.Length - 2, 2);
            sb.Append(" ]");
            return sb.ToString();
        }
    }
}
